package com.example.administrator.familfinalcialmanagement.ui.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;

import com.example.administrator.familfinalcialmanagement.R;
import com.example.administrator.familfinalcialmanagement.bean.LoginBean;
import com.example.administrator.familfinalcialmanagement.bean.OrderBean;
import com.example.administrator.familfinalcialmanagement.bean.OrderChildBean;
import com.example.administrator.familfinalcialmanagement.databinding.ActivityMyOrdersBinding;
import com.example.administrator.familfinalcialmanagement.ui.BaseActivity;
import com.example.administrator.familfinalcialmanagement.ui.adapter.FriendAdapter;
import com.example.administrator.familfinalcialmanagement.util.ApiHTTP;
import com.example.administrator.familfinalcialmanagement.util.GsonUtil;
import com.example.administrator.familfinalcialmanagement.util.LogUtil;
import com.example.administrator.familfinalcialmanagement.util.SpUtils;
import com.example.administrator.familfinalcialmanagement.util.ToastUtil;
import com.lzy.okgo.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class MyOrdersActivity extends BaseActivity {

    ActivityMyOrdersBinding binding;
    List<OrderBean.DataBean.OrderListBean> list = new ArrayList<>();
    List<OrderBean.DataBean.OrderListBean> listSecond = new ArrayList<>();
    FriendAdapter adapter;
    LoginBean bean = (LoginBean) SpUtils.getBean( "module_loginBean" );

    /**
     * 1 未发货
     * 2 已发货
     * 3 已完成
     * 4 已支付
     */
    String type;

    String stroeStype  = "" ,payStype = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        binding = DataBindingUtil.setContentView( this, R.layout.activity_my_orders );
        binding.setActivity( this );
        initView();

    }

    private void initView() {
        setBack();
        setTitleText( "我的订单" );

        type = getIntent().getStringExtra( "module_type" );
        switch (type) {
            case "1":
                stroeStype = "0";
                break;
            case "2":
                stroeStype = "1";
                break;
            case "3":
                stroeStype = "2";
                break;
            case "4":
                payStype = "1";
                break;
            default:
        }

        queryOrders(stroeStype,payStype);
        adapter = new FriendAdapter( MyOrdersActivity.this, list );
        binding.recycler.setLayoutManager( new LinearLayoutManager( MyOrdersActivity.this ) );
        binding.recycler.setAdapter( adapter );
    }

    public void queryOrders(String stroeStype,String payStype) {
        ApiHTTP.getInstance().queryOrders( bean.getData().getId() + "",
                stroeStype, payStype, "1", new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        LogUtil.e( "联网成功"+ s );
                        OrderBean bean = GsonUtil.GsonToBean(s,OrderBean.class);
                        if (0 == bean.getCode()) {
                            listSecond = bean.getData().getOrderList();
                            list.clear();
                            list.addAll(listSecond);
                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        ToastUtil.showToast( "联网失败" );
                    }
                } );
    }
}
